//只能支持两个按钮单选
import 'package:flutter/material.dart';

class RadioGroupWidget extends StatefulWidget {
  List<String> datas;

  Function(String) onSelected;
  double radioWidth = 80;
  double radioHeight = 28;

  RadioGroupWidget({
    @required this.datas,
    @required this.onSelected,
    this.radioWidth,
    this.radioHeight,
  });

  @override
  State<StatefulWidget> createState() {
    return RadioGroupState();
  }
}

class RadioGroupState extends State<RadioGroupWidget> {
  var chooseStr;
  int choosed = 1;
  Color choosedBgColor = Colors.blue;
  Color choosedCornerColor = Colors.blue;
  Color choosedTxtColor = Colors.white;
  Color defaultBgColor = Colors.white;
  Color defaultCornerColor = Colors.grey;
  Color defaultTxtColor = Colors.grey;

  @override
  void initState() {
    super.initState();
    chooseStr = widget.datas[0];
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        GestureDetector(
            onTap: () {
              choosed = 1;
              chooseStr = widget.datas[0];
              setState(() {});
              widget.onSelected(chooseStr);
            },
            child: Container(
              height: widget.radioHeight,
              width: widget.radioWidth,
              decoration: BoxDecoration(
                color: choosed == 1 ? choosedBgColor : defaultBgColor,
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(15),
                    bottomLeft: Radius.circular(15)),
                border: Border.all(
                    width: 1,
                    color:
                        choosed == 1 ? choosedCornerColor : defaultCornerColor),
              ),
              child: Center(
                  child: Text(widget.datas[0],
                      style: TextStyle(
                          color:
                              choosed == 1 ? choosedTxtColor : defaultTxtColor,
                          fontSize: 12))),
            )),
        GestureDetector(
            onTap: () {
              choosed = 2;
              chooseStr = widget.datas[1];
              setState(() {});
              widget.onSelected(chooseStr);
            },
            child: Container(
              height: widget.radioHeight,
              width: widget.radioWidth,
              decoration: BoxDecoration(
                color: choosed == 2 ? choosedBgColor : defaultBgColor,
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(15),
                    bottomRight: Radius.circular(15)),
                border: Border.all(
                    width: 1,
                    color:
                        choosed == 2 ? choosedCornerColor : defaultCornerColor),
              ),
              child: Center(
                  child: Text(widget.datas[1],
                      style: TextStyle(
                          color:
                              choosed == 2 ? choosedTxtColor : defaultTxtColor,
                          fontSize: 12))),
            ))
      ],
    );
  }
}
